/**
 * @file frontend.py
 * @brief Frontend page routes for Meeting Management System.
 * @author Ziyi Wang
 *
 * Provides HTML page rendering for login, register, dashboard, meetings, rooms, profile, and settings.
 */
from fastapi import APIRouter, Request, Depends, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
import os
from pathlib import Path

from ..auth import get_current_active_user
from ..models.models import User as UserModel

# 获取模板目录的绝对路径
BASE_DIR = Path(__file__).resolve().parent.parent.parent
templates = Jinja2Templates(directory=str(BASE_DIR / "templates"))

router = APIRouter(tags=["frontend"])

/**
 * @brief Render login page.
 * @param request FastAPI request object
 * @return login.html template
 */
@router.get("/", response_class=HTMLResponse)
async def root(request: Request):
    return templates.TemplateResponse("login.html", {"request": request})

/**
 * @brief Render login page.
 * @param request FastAPI request object
 * @return login.html template
 */
@router.get("/login", response_class=HTMLResponse)
async def login(request: Request):
    return templates.TemplateResponse("login.html", {"request": request})

/**
 * @brief Render register page.
 * @param request FastAPI request object
 * @return register.html template
 */
@router.get("/register", response_class=HTMLResponse)
async def register(request: Request):
    return templates.TemplateResponse("register.html", {"request": request})

/**
 * @brief Render dashboard page.
 * @param request FastAPI request object
 * @return dashboard.html template
 */
@router.get("/dashboard", response_class=HTMLResponse)
async def dashboard(request: Request):
    user = None
    is_admin = False
    try:
        from fastapi import Header
        from ..auth import get_current_active_user
        from fastapi import Depends
        # 尝试获取用户信息（如果有token）
        # 这里实际生产环境应通过session/cookie或前端js判断
    except Exception:
        pass
    return templates.TemplateResponse("dashboard.html", {
        "request": request,
        "is_admin": is_admin
    })

/**
 * @brief Render meetings list page.
 * @param request FastAPI request object
 * @return meetings.html template
 */
@router.get("/meetings", response_class=HTMLResponse)
async def meetings(request: Request):
    return templates.TemplateResponse("meetings.html", {
        "request": request,
        "is_admin": False
    })

/**
 * @brief Render my meetings page.
 * @param request FastAPI request object
 * @return my_meetings.html template
 */
@router.get("/my-meetings", response_class=HTMLResponse)
async def my_meetings(request: Request):
    return templates.TemplateResponse("my_meetings.html", {
        "request": request,
        "is_admin": False
    })

/**
 * @brief Render create meeting page.
 * @param request FastAPI request object
 * @return create_meeting.html template
 */
@router.get("/meetings/create", response_class=HTMLResponse)
async def create_meeting(request: Request):
    return templates.TemplateResponse("create_meeting.html", {
        "request": request,
        "is_admin": False
    })

/**
 * @brief Render meeting detail page.
 * @param request FastAPI request object
 * @param meeting_id Meeting ID
 * @return meeting_detail.html template
 */
@router.get("/meetings/{meeting_id}", response_class=HTMLResponse)
async def meeting_detail(request: Request, meeting_id: int):
    return templates.TemplateResponse("meeting_detail.html", {
        "request": request,
        "meeting_id": meeting_id,
        "is_admin": False
    })

/**
 * @brief Render rooms list page.
 * @param request FastAPI request object
 * @return rooms.html template
 */
@router.get("/rooms", response_class=HTMLResponse)
async def rooms(request: Request):
    return templates.TemplateResponse("rooms.html", {
        "request": request,
        "is_admin": False
    })

/**
 * @brief Render create room page.
 * @param request FastAPI request object
 * @return room_create.html template
 */
@router.get("/rooms/create", response_class=HTMLResponse)
async def create_room(request: Request):
    return templates.TemplateResponse("room_create.html", {
        "request": request,
        "is_admin": True
    })

/**
 * @brief Render room detail page.
 * @param request FastAPI request object
 * @param room_id Room ID
 * @return room_detail.html template
 */
@router.get("/rooms/{room_id}", response_class=HTMLResponse)
async def room_detail(request: Request, room_id: int):
    return templates.TemplateResponse("room_detail.html", {
        "request": request,
        "room_id": room_id,
        "is_admin": False
    })

/**
 * @brief Render room edit page.
 * @param request FastAPI request object
 * @param room_id Room ID
 * @return room_edit.html template
 */
@router.get("/rooms/{room_id}/edit", response_class=HTMLResponse)
async def room_edit(request: Request, room_id: int):
    return templates.TemplateResponse("room_edit.html", {
        "request": request,
        "room_id": room_id,
        "is_admin": True
    })

/**
 * @brief Render profile page.
 * @param request FastAPI request object
 * @return profile.html template
 */
@router.get("/profile", response_class=HTMLResponse)
async def profile(request: Request):
    return templates.TemplateResponse("profile.html", {
        "request": request,
        "is_admin": False
    })

/**
 * @brief Render settings page.
 * @param request FastAPI request object
 * @return settings.html template
 */
@router.get("/settings", response_class=HTMLResponse)
async def settings(request: Request):
    return templates.TemplateResponse("settings.html", {
        "request": request,
        "is_admin": False
    }) 